package star.cloud.asme.asmePerson.asmePersonTrain.asmePersonTrainInstrutor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.file.fileInfo.FileInfo;
import star.cloud.asme.base.file.fileInfo.FileInfoMapper;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;
import star.cloud.asme.base.utils.ObjectsUtil;
import star.cloud.asme.system.user.User;
import star.cloud.asme.system.user.UserMapper;

import java.util.UUID;


@Service
@Slf4j
@RequiredArgsConstructor
public class AsmePersonTrainInstructorServiceImpl extends IBaseServiceImpl<AsmePersonTrainInstructorMapper, AsmePersonTrainInstructor> implements AsmePersonTrainInstructorService {

    private final AsmePersonTrainInstructorMapper asmePersonTrainInstructorMapper;
    private final FileInfoMapper fileInfoMapper;
    private final UserMapper userMapper;

    @Override
    public Result<AsmePersonTrainInstructor> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<AsmePersonTrainInstructor> queryWrapper = new LambdaQueryWrapper<>();
        if (columnName.equals("id")) {
            queryWrapper.eq(AsmePersonTrainInstructor::getId, columnValue);
        }
        AsmePersonTrainInstructor asmePersonTrainInstructor = asmePersonTrainInstructorMapper.selectOne(queryWrapper);
        return Result.ok(asmePersonTrainInstructor);
    }

    @Override
    public Result<IPage<AsmePersonTrainInstructor>> getListByConditionsWithPage(AsmePersonTrainInstructor conditions) {
        IPage<AsmePersonTrainInstructor> iPage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        LambdaQueryWrapper<AsmePersonTrainInstructor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getMethod()), AsmePersonTrainInstructor::getMethod, conditions.getMethod());
        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getUsername()), AsmePersonTrainInstructor::getUsername, conditions.getUsername());

        asmePersonTrainInstructorMapper.selectPage(iPage, queryWrapper);
        iPage.getRecords().forEach(asmePersonTrainInstructor -> {
            FileInfo fileInfo = fileInfoMapper.selectOneByKeyWord(asmePersonTrainInstructor.getAttachmentUuid());
            if (fileInfo != null) {
                asmePersonTrainInstructor.setAttachmentUrl(fileInfo.getUrl());
            }
            User user = userMapper.selectOneByUsername(asmePersonTrainInstructor.getUsername());
            if (user != null) {
                asmePersonTrainInstructor.setUser(user);
            }
            setCreatorAndUpdater(asmePersonTrainInstructor);
        });
        return Result.ok(iPage);
    }

    @Override
    public Result<AsmePersonTrainInstructor> insertOne(AsmePersonTrainInstructor entity) {
        if (checkRepeat(entity)) {
            return Result.error(entity).msg("信息重复");
        }
        entity.setAttachmentUuid(UUID.randomUUID().toString());
        int inserted = asmePersonTrainInstructorMapper.insert(entity);
        setCreatorAndUpdater(entity);
        return inserted > 0 ? Result.ok(entity) : Result.error();
    }

    @Override
    public Result<AsmePersonTrainInstructor> updateOne(AsmePersonTrainInstructor entity) {
        setVersion(entity);
        int updated = asmePersonTrainInstructorMapper.updateById(entity);
        setCreatorAndUpdater(entity);
        return updated > 0 ? Result.ok(entity) : Result.error();
    }

    private Boolean checkRepeat(AsmePersonTrainInstructor asmePersonTrainInstructor) {
        LambdaQueryWrapper<AsmePersonTrainInstructor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AsmePersonTrainInstructor::getUsername, asmePersonTrainInstructor.getUsername());
        queryWrapper.eq(AsmePersonTrainInstructor::getMethod, asmePersonTrainInstructor.getMethod());
        AsmePersonTrainInstructor selectedOne = asmePersonTrainInstructorMapper.selectOne(queryWrapper);
        return selectedOne != null;
    }

}
